<template>
  <div id="examinpaper_previewContainer" class="container">
    <div class="content">
      <el-breadcrumb separator-class="el-icon-arrow-right">
        <el-breadcrumb-item class="title" style=" font-size: 0.2rem;" :to="{ path: toPath }">试卷管理</el-breadcrumb-item>
        <el-breadcrumb-item class="title" style=" font-size: 0.2rem;">预览试卷</el-breadcrumb-item>
      </el-breadcrumb>
      <div class="preview">
        <div class="preview_info">
          <div class="preview_title">
            <span>{{previewData.title}}</span>
          </div>
          <div class="preview_mark">
            <div style="float:left;">适用年龄：{{previewData.ageStart}}-{{previewData.ageEnd}}岁</div>
            <div style="float:right;">
              总分：
              <span>{{totalScore}}</span>
            </div>
          </div>
        </div>
        <audio ref="audio" :src="audioUrl"></audio>
        <div>
          <div class="preview_subject" v-for="item,index in previewData.evSubjects">
            <div class="subject_title">
              <div class="subject_title_left">{{item.sort}}.{{item.title}}</div>
              <div class="subject_title_right">
                <el-image
                  v-if="hasApi('examinpaperDeleteSubject')"
                  class="title_right_btn"
                  :src="require('@/assets/img/common/paper_delete.png')"
                  fit="cover"
                  @click="delete_subject(item)"
                ></el-image>
                <el-image
                  v-if="hasApi('examinpaperUpdateSubject')"
                  class="title_right_btn"
                  :src="require('@/assets/img/common/paper_update.png')"
                  @click="update_subject(item)"
                  fit="cover"
                ></el-image>
                <el-image
                  v-if="hasApi('examinpaperAddSubject')"
                  class="title_right_btn"
                  :src="require('@/assets/img/common/paper_add.png')"
                  @click="add_subject(item)"
                  fit="cover"
                ></el-image>
                <div
                  class="title_score"
                  :class="{title_score_rightLine:hasApi('examinpaperAddSubject')}"
                >总分：{{item.score}}分</div>
                <!-- <el-button type="primary" style="width:0.4rem;">编辑</el-button>
              <el-button type="primary" style="width:0.4rem;">添加</el-button>
                <el-button type="primary" style="width:0.4rem;">删除</el-button>-->
              </div>
            </div>
            <div class="test_stem" v-html="item.content"></div>
            <div v-for="sound in item.fileList" class="soundPlay">
              <el-image
                style="width: 0.2rem; height: 0.14rem;margin-right:0.06rem;"
                :src="require('@/assets/img/common/audio.png')"
                fit="cover"
              ></el-image>
              <div class="soundName">{{sound.name}}</div>
              <el-image
                v-if="!sound.isPlay"
                style="width: 0.22rem; height: 0.22rem;cursor: pointer;margin-left:0.1rem;"
                :src="require('@/assets/img/common/play.png')"
                @click="play(sound)"
                fit="cover"
              ></el-image>
              <el-image
                v-if="sound.isPlay"
                style="width: 0.22rem; height: 0.22rem;cursor: pointer;margin-left:0.1rem;"
                :src="require('@/assets/img/common/suspend.png')"
                @click="pause(sound)"
                fit="cover"
              ></el-image>
            </div>

            <div class="subject_question" v-for="question,index in item.questions">
              <div class="question_title">
                <div class="index">
                  <div class="num" v-if="index<10">{{'0'+(index+1)}}</div>
                  <div class="num" v-else>{{index+1}}</div>
                </div>
                <div>
                  <div class="title_name" v-if="question.title !=''">{{question.title}}</div>
                  <div v-for="sound in question.fileList" class="soundPlay">
                    <el-image
                      style="width: 0.2rem; height: 0.14rem;margin-right:0.06rem;"
                      :src="require('@/assets/img/common/audio.png')"
                      fit="cover"
                    ></el-image>
                    <div class="soundName">{{sound.name}}</div>
                    <el-image
                      v-if="!sound.isPlay"
                      style="width: 0.22rem; height: 0.22rem;cursor: pointer;margin-left:0.1rem;"
                      :src="require('@/assets/img/common/play.png')"
                      @click="play(sound)"
                      fit="cover"
                    ></el-image>
                    <el-image
                      v-if="sound.isPlay"
                      style="width: 0.22rem; height: 0.22rem;cursor: pointer;margin-left:0.1rem;"
                      :src="require('@/assets/img/common/suspend.png')"
                      @click="pause(sound)"
                      fit="cover"
                    ></el-image>
                  </div>
                </div>
                <!-- <el-image
                v-if="question.sound !=''"
                style="width: 0.28rem; height: 0.22rem;cursor: pointer;margin-bottom:0.04rem;margin-right:0.1rem;"
                :src="require('@/assets/img/common/audio.png')"
                fit="cover"
                ></el-image>-->
                <!-- <el-button type="danger" v-if="question.sound !=''" @click="play(question)">播放按钮</el-button> -->
              </div>
              <div class="answers" v-if="question.type == 1">
                <div>
                  <div
                    class="answers_title"
                    v-for="answer,index in question.answers"
                  >{{charCode[index]}}.&nbsp;{{answer.title}}</div>
                </div>
                <!-- <div v-else>
                  <span v-if="checkHasFeed(question.answers)"
                    v-for="answer,index in question.answers"
                    class="answers_title"
                  >{{charCode[index]}}.{{answer.title}}</span>
                </div>-->
                <!-- <span
                
                class="answers_title"
                >{{charCode[index]}}.{{answer.title}}</span>-->
              </div>
              <div class="right_answer" v-if="question.type == 1">
                正确答案：
                <span>{{getRightKey(question)}}</span>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
var vm, $audio;
import wangeditor from "../operation_manage/common/wangeditor";
export default {
  components: { wangeditor },
  data: function() {
    return {
      previewData: {},
      charCode: [],
      audioUrl: "",
      totalScore: 0,
      toPath: "",
      paperId: "",
      currentPlayData: {}
    };
  },
  methods: {
    /**校验选项是否换行 */
    checkHasFeed(list) {
      let a = false;
      list.forEach(item => {
        if (item.title.length > 2) {
          a = true;
        }
      });
      return a;
    },
    /**删除题目 */
    delete_subject(item) {
      if (vm.previewData.reportCount + vm.previewData.startCount > 0) {
        vm.$confirm(
          "已存在测评数据的试卷无法编辑，是否拷贝为新的试卷再编辑？",
          "拷贝试卷",
          {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            center: true,
            showClose: false,
            type: "warning"
          }
        )
          .then(e => {
            api
              .evaluateReportCopy({ paperId: vm.paperId })
              .then(function(data) {
                if (data.resultCode == 2000) {
                  vm.$alert(
                    "原试卷已下架，请编辑完成后对当前试卷进行上架。",
                    "拷贝试卷成功",
                    {
                      confirmButtonText: "确定",
                      type: "warning",
                      center: true,
                      callback: action => {
                        vm.paperId = data.paperId;
                        vm.initData();
                      }
                    }
                  );
                } else {
                  vm.$message.error(data.message);
                }
              });
          })
          .catch(e => {});
      } else {
        vm.$confirm("确认删除所选试题吗？", "删除", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          center: true,
          showClose: false,
          type: "warning"
        })
          .then(e => {
            let paras = {
              subjectId: item.subjectId,
              paperId: vm.paperId
            };
            api.examinpaperDeleteSubject(paras).then(function(data) {
              if (data.resultCode == 2000) {
                vm.$message({
                  message: "删除成功",
                  type: "success"
                });
                vm.initData();
              } else {
                vm.$message.error(data.message);
              }
            });
          })
          .catch(e => {});
      }
    },
    /**新增题目 */
    add_subject(item) {
      console.log(vm.previewData);
      if (vm.previewData.reportCount + vm.previewData.startCount > 0) {
        vm.$confirm(
          "已存在测评数据的试卷无法编辑，是否拷贝为新的试卷再编辑？",
          "拷贝试卷",
          {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            center: true,
            showClose: false,
            type: "warning"
          }
        )
          .then(e => {
            api
              .evaluateReportCopy({ paperId: vm.paperId })
              .then(function(data) {
                if (data.resultCode == 2000) {
                  vm.$alert(
                    "原试卷已下架，请编辑完成后对当前试卷进行上架。",
                    "拷贝试卷成功",
                    {
                      confirmButtonText: "确定",
                      type: "warning",
                      center: true,
                      callback: action => {
                        vm.paperId = data.paperId;
                        vm.initData();
                      }
                    }
                  );
                } else {
                  vm.$message.error(data.message);
                }
              });
          })
          .catch(e => {});
      } else {
        var path = vm.$store.state.permit["104"].path;
        //传递参数
        this.$router.push({
          path: path,
          query: {
            paperId: vm.previewData.paperId,
            subjectId: item.subjectId,
            isFromPreview: true,
            index: item.sort
          }
        });
      }
    },
    /**编辑题目 */
    update_subject(item) {
      console.log(vm.previewData);
      if (vm.previewData.reportCount + vm.previewData.startCount > 0) {
        vm.$confirm(
          "已存在测评数据的试卷无法编辑，是否拷贝为新的试卷再编辑？",
          "拷贝试卷",
          {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            center: true,
            showClose: false,
            type: "warning"
          }
        )
          .then(e => {
            api
              .evaluateReportCopy({ paperId: vm.paperId })
              .then(function(data) {
                if (data.resultCode == 2000) {
                  vm.$alert(
                    "原试卷已下架，请编辑完成后对当前试卷进行上架。",
                    "拷贝试卷成功",
                    {
                      confirmButtonText: "确定",
                      type: "warning",
                      center: true,
                      callback: action => {
                        vm.paperId = data.paperId;
                        vm.initData();
                      }
                    }
                  );
                } else {
                  vm.$message.error(data.message);
                }
              });
          })
          .catch(e => {});
      } else {
        var path = vm.$store.state.permit["104"].path;
        //传递参数
        this.$router.push({
          path: path,
          query: {
            paperId: vm.previewData.paperId,
            subjectId: item.subjectId,
            index: item.sort
          }
        });
      }
    },
    /**返回正确答案 */
    getRightKey(question) {
      let str = "";
      if (question.answers != null) {
        question.answers.forEach((item, index) => {
          if (item.isCorrect == "1") {
            str = vm.charCode[index];
          }
        });
      }
      return str;
    },
    play(item) {
      if (vm.currentPlayData && vm.currentPlayData.isPlay) {
        vm.currentPlayData.isPlay = false;
        this.$refs.audio.pause();
      }
      item.isPlay = true;
      if (
        vm.currentPlayData &&
        vm.currentPlayData.questionId == item.questionId
      ) {
        vm.currentPlayData.isPlay = true;
      } else {
        vm.currentPlayData = item;
        this.$refs.audio.src = item.url;
      }
      this.$refs.audio.play(); //开始播放
      this.$refs.audio.addEventListener(
        "ended",
        function() {
          vm.currentPlayData.isPlay = false;
          item.isPlay = false;
        },
        false
      );
    },
    pause(item) {
      item.isPlay = false;
      vm.currentPlayData.isPlay = false;
      this.$refs.audio.pause(); //开始播放
    },
    initData() {
      vm.totalScore = 0;
      api.examinpaperListNopage({ paperId: vm.paperId }).then(function(data) {
        if (data.resultCode == 2000) {
          data.list[0].evSubjects.forEach(item => {
            item.fileList = [];
            if (item.sound && item.sound != "") {
              let soundArr = item.sound.split(",");
              for (let i = 0; i < soundArr.length; i++) {
                // let
                item.fileList.push({
                  name: soundArr[i].substring(soundArr[i].lastIndexOf("/") + 1),
                  url: soundArr[i],
                  questionId: tool.uuid(),
                  isPlay: false
                });
              }
            }

            vm.totalScore += item.score;
            item.questions.forEach(question => {
              question.fileList = [];
              if (question.sound && question.sound != "") {
                let soundArr = question.sound.split(",");
                for (let i = 0; i < soundArr.length; i++) {
                  // let
                  question.fileList.push({
                    name: soundArr[i].substring(
                      soundArr[i].lastIndexOf("/") + 1
                    ),
                    url: soundArr[i],
                    questionId: tool.uuid(),
                    isPlay: false
                  });
                }
              }
            });
          });

          vm.previewData = data.list[0];
        } else {
          vm.$message.error(data.message);
        }
      });
    }
  },
  mounted() {
    vm.paperId = this.$route.query.paperId;
    let isFromCampus = this.$route.query.isFromCampus;
    if (isFromCampus) {
      vm.toPath = "/evaluation_manage/examinpaper_manage_campus";
    } else {
      vm.toPath = "/evaluation_manage/examinpaper_manage";
    }
    //初始化
    vm.initData();
  },
  created: function() {
    vm = this;
    vm.charCode = config.charCode;
  }
};
</script>
<style lang="scss">
#examinpaper_previewContainer {
  .el-form-item__label {
    font-size: 0.16rem;

    color: #666666;
  }

  .el-checkbox__label {
    font-size: 0.16rem;
    color: #666666;
  }

  .el-breadcrumb__separator {
    color: #999999;
  }
  .el-breadcrumb__inner.is-link {
    font-weight: 300;
    color: #333333;
  }
  .el-breadcrumb__inner.is-link:hover {
    color: #3885f8;
  }
  .el-breadcrumb__item:last-child .el-breadcrumb__inner {
    color: #3885f8;
  }
  .newclassroomNumber {
    .el-input__inner {
      border-radius: 0.04rem !important;
    }
  }
  .preview {
    width: 100%;
    height: 100%;
    background-color: #ffffff;
    font-size: 0.16rem;
    color: #666666;
    border-radius: 0.06rem;
    padding: 0.1rem 0.2rem 0.2rem 0.2rem;
    .preview_info {
      padding: 0.1rem 0 0.04rem 0;
      overflow: hidden;
      .preview_title {
        width: 100%;
        text-align: center;
        color: #000000;
        font-size: 0.24rem;
        font-weight: 600;
      }
      .preview_mark {
        width: 4rem;
        margin: 0 auto;
        color: #333333;
        font-size: 0.16rem;
        overflow: hidden;
        padding-top: 0.2rem;
        padding-bottom: 0.1rem;
        span {
          color: #ff4200;
        }
      }
    }

    .preview_subject {
      border: solid 1px #ebebeb;
      border-bottom: 0;
      font-size: 0.16rem;
      color: #666666;
      // border-radius: 0.04rem;
      // margin-bottom: 0.2rem;
      .subject_title {
        overflow: hidden;
        border-bottom: solid 1px #ebebeb;
        background-color: #f9fafb;
        height: 0.5rem;
        .subject_title_left {
          float: left;
          padding-top: 0.12rem;
          padding-left: 0.25rem;
          font-weight: 600;
          width: 70%;
          color: #666666;
        }
        .subject_title_right {
          float: right;
          width: 20%;
          overflow: hidden;
          text-align: center;
          .title_score {
            float: right;
            padding-top: 0.13rem;
            padding-bottom: 0.12rem;
            padding-right: 0.1rem;
            margin-right: 0.2rem;
          }
          .title_score_rightLine {
            border-right: 1px solid #f1f1f3;
          }
          .title_right_btn {
            margin-top: 0.12rem;
            float: right;
            width: 0.24rem;
            height: 0.24rem;
            cursor: pointer;
            margin-right: 0.12rem;
          }
        }
      }
      .test_stem {
        margin: 0 auto;
        width: 1093px;
        max-height: 394px;
        overflow: auto;
        color: #000000;
    font-size: 18px;
      }
      /*滚动条样式*/
      .test_stem::-webkit-scrollbar {
        width: 0.04rem;
      }
      .test_stem::-webkit-scrollbar-thumb {
        border-radius: 0.5rem;
        background: #bbbbbb;
      }
      .test_stemtest_stem::-webkit-scrollbar-track {
        background: #f2f6fc;
      }
      .soundPlay {
        display: flex;
        justify-content: center;
        align-items: center;
        color: #0e1115;
        font-size: 0.18rem;
        .soundName {
          height: 0.38rem;
          line-height: 0.38rem;
        }
      }
      .subject_question {
        .question_title {
          position: relative;
          background-color: #ddeaf8;
          // display: flex;
          // justify-content: flex-start;
          // align-items: center;
          font-size: 0.18rem;
          color: #0e1115;
          .index {
            position: absolute;
            top: 0;
            bottom: 0;
            background-color: #6d98d9;
            width: 0.68rem;
            .num {
              position: absolute;
              width: 0.68rem;
              height: 0.38rem;
              top: calc(50% - 0.19rem);
              left: 0;
              text-align: center;
              color: #fefeff;
              padding-top: 0.06rem;
              font-weight: 600;
            }
          }
          .title_name {
            margin-left: 0.88rem;
            height: 0.38rem;
            line-height: 0.38rem;
          }
          .soundPlay {
            margin-left: 0.88rem;
            display: flex;
            justify-content: flex-start;
            align-items: center;
            .soundName {
              height: 0.38rem;
              line-height: 0.38rem;
            }
          }
        }

        .answers {
          width: 100%;
          padding-top: 0.1rem;
          padding-left: 0.88rem;
          color: #0e1115;
          background-color: #f9fafb;
          font-size: 18px;
          .answers_title {
            padding: 0.04rem 0rem;
            cursor: pointer;
          }
        }
        .right_answer {
          background-color: #f9fafb;
          padding: 0.06rem 0.32rem 0.2rem 0.32rem;
          span {
            font-size: 0.2rem;
            color: #333333;
            margin-left: 0.1rem;
            font-weight: 500;
          }
        }
      }
    }
    .preview_subject:first-child {
      border-top-left-radius: 0.04rem;
      border-top-right-radius: 0.04rem;
    }
    .preview_subject:last-child {
      border-bottom-right-radius: 0.04rem;
      border-bottom-left-radius: 0.04rem;
      border-bottom: solid 1px #ebebeb;
    }
  }
}
</style>
<style scoped src="@/style/module/table.css"></style>
<style scoped src="@/style/clue_manage/clue_manage.css"></style>
<style scoped>
</style>
